package com.rain.search.service;

import com.rain.search.dao.ArticleSearchDao;
import com.rain.search.pojo.Article;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;
import utils.IdWorker;

/**
 * @auther 德哲
 * @date 2019/4/28 21:13.
 */
@Service
public class ArticleService {

    @Autowired
    private ArticleSearchDao articleSearchDao;

    @Autowired
    private IdWorker idWorker;

    /**
     * 保存方法
     * @param article
     */
    public void save(Article article){
        article.setId(String.valueOf(idWorker.nextId()));
        articleSearchDao.save(article);
    }

    /**
     * 使用关键字查询并分页
     * @param keywords
     * @param page
     * @param size
     * @return
     */
    public Page<Article> findByKeywords(String keywords,int page,int size){
        PageRequest pageRequest = PageRequest.of(page-1,size);
        return articleSearchDao.findByTitleOrContentLike(keywords, keywords, pageRequest);

    }

}
